package com.lenovo.smarttraffic.ui.activity.stage6;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lenovo.smarttraffic.MainActivity;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.CarPeccancyBean;
import com.lenovo.smarttraffic.bean.PeccancyTypeBean;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.util.HttpUtils;
import com.lenovo.smarttraffic.util.ToastUtils;
import com.lenovo.smarttraffic.util.Utils;

import java.util.ArrayList;
import java.util.List;

public class CarFindActivity extends BaseActivity implements ScaleGestureDetector.OnScaleGestureListener {

    public static CarFindActivity carFindActivity;

    public static List<CarPeccancyBean.ROWSDETAILBean> list = new ArrayList<>();
    public static List<PeccancyTypeBean.ROWSDETAILBean> type_list = new ArrayList<>();
    public static List<String> id_list = new ArrayList<>();

    private LinearLayout find_parent;
    private EditText car_et;
    private Button find_btn;

    private LinearLayout result_parent;
    private View add;
    private ListView id_lv, info_lv;

    private LinearLayout pic_parent;
    private GridLayout pics_parent;
    private View wz1, wz2, wz3, wz4;
    private ImageView car_img;
    private Button back_btn;

    private Handler handler = new Handler();

    private ScaleGestureDetector scaleGestureDetector;
    //矩阵对象
    private Matrix scaleMatrix = new Matrix();

    @Override
    protected int getLayout() {
        return R.layout.activity_car_find;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        carFindActivity = this;
        initToolBar(this, "车辆违章");

        find_parent = findViewById(R.id.find_parent);
        car_et = findViewById(R.id.car_et);
        find_btn = findViewById(R.id.find_btn);

        result_parent = findViewById(R.id.result_parent);
        add = findViewById(R.id.add);
        id_lv = findViewById(R.id.id_lv);
        info_lv = findViewById(R.id.info_lv);

        pic_parent = findViewById(R.id.pic_parent);
        pics_parent = findViewById(R.id.pics_parent);
        wz1 = findViewById(R.id.wz1);
        wz2 = findViewById(R.id.wz2);
        wz3 = findViewById(R.id.wz3);
        wz4 = findViewById(R.id.wz4);
        car_img = findViewById(R.id.car_img);
        back_btn = findViewById(R.id.back_btn);

        find_btn.setOnClickListener(v -> {
            String car_id = car_et.getText().toString();
            if (!car_id.startsWith("B100") || car_id.length() < 6) {
                ToastUtils.showToast(this, "车牌号格式不正确");
            } else {
                ProgressDialog progressDialog = new ProgressDialog(this);
                progressDialog.setMessage("请求网络中");
                progressDialog.setCancelable(false);
                progressDialog.show();

                String car_id2 = "鲁" + car_id;

                handler.postDelayed(() -> HttpUtils.post("get_car_peccancy", new HttpUtils().add("UserName", MainActivity.userName).add("carnumber", car_id2), response ->
                        HttpUtils.post("get_peccancy_type", new HttpUtils().setParam("UserName", MainActivity.userName),
                                response2 -> {
                                    if (Utils.getResult(response)) {
                                        ToastUtils.showToast(this, "网络请求成功");

                                        type_list = new Gson().fromJson(response2.toString(), PeccancyTypeBean.class).getROWS_DETAIL();
                                        if ((list = new Gson().fromJson(response.toString(), CarPeccancyBean.class).getROWS_DETAIL()).isEmpty()) {
                                            ToastUtils.showToast(CarFindActivity.this, "没有查询到" + car_id2 + "车的违章数据！");
                                        } else {
                                            if (!id_list.contains(car_id2)) {
                                                id_list.add(car_id2);
                                                id_lv.setAdapter(new CarFindAdapter(1));
                                                info_lv.setAdapter(new CarFindAdapter(2));
                                            }
                                            showView(2);
                                        }
                                    } else {
                                        ToastUtils.showToast(this, "网络请求失败");
                                    }
                                    progressDialog.dismiss();
                                })), 1000);
            }
        });

        add.setOnClickListener(v -> showView(1));

        wz1.setOnClickListener(v -> {
            car_img.setImageDrawable(getDrawable(R.mipmap.weizhang01));
            showView(4);
        });
        wz2.setOnClickListener(v -> {
            car_img.setImageDrawable(getDrawable(R.mipmap.weizhang02));
            showView(4);
        });
        wz3.setOnClickListener(v -> {
            car_img.setImageDrawable(getDrawable(R.mipmap.weizhang03));
            showView(4);
        });
        wz4.setOnClickListener(v -> {
            car_img.setImageDrawable(getDrawable(R.mipmap.weizhang04));
            showView(4);
        });

        scaleGestureDetector = new ScaleGestureDetector(this, this);
        car_img.setOnTouchListener((v, event) -> scaleGestureDetector.onTouchEvent(event));

        back_btn.setOnClickListener(v -> showView(2));
    }

    public void showView(int which) {
        if (which == 1) {
            initToolBar(this, "车辆违章");
            find_parent.setVisibility(View.VISIBLE);
            result_parent.setVisibility(View.GONE);
        } else if (which == 2) {
            initToolBar(this, "查询结果");
            find_parent.setVisibility(View.GONE);
            result_parent.setVisibility(View.VISIBLE);
            pic_parent.setVisibility(View.GONE);
            car_img.setVisibility(View.GONE);
        } else if (which == 3) {
            initToolBar(this, "监控抓拍");
            result_parent.setVisibility(View.GONE);
            pic_parent.setVisibility(View.VISIBLE);
            pics_parent.setVisibility(View.VISIBLE);
        } else {
            pics_parent.setVisibility(View.GONE);
            car_img.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        float scaleFactor = detector.getScaleFactor();
        if (car_img == null) return true;
        scaleMatrix.postScale(scaleFactor, scaleFactor, car_img.getWidth() / 2, car_img.getHeight() / 2);
        car_img.setImageMatrix(scaleMatrix);
        return true;
    }

    @Override
    public boolean onScaleBegin(ScaleGestureDetector detector) {
        return true;
    }

    @Override
    public void onScaleEnd(ScaleGestureDetector detector) {

    }
}

class CarFindAdapter extends BaseAdapter {

    private List<CarPeccancyBean.ROWSDETAILBean> list = new ArrayList<>();
    private List<PeccancyTypeBean.ROWSDETAILBean> type_list = CarFindActivity.type_list;
    private List<String> id_list = CarFindActivity.id_list;

    private int type;

    private int count = 0;

    public CarFindAdapter(int type) {
        this.type = type;

        //Init list
        calcCount();

    }

    private void calcCount() {
        count = 0;
        for (int i = 0; i < id_list.size(); i++) {
            for (int j = 0; j < CarFindActivity.list.size(); j++) {
                if (id_list.get(i).equals(CarFindActivity.list.get(j).getCarnumber())) {
                    list.add(CarFindActivity.list.get(j));
                    count++;
                }
            }
        }
    }

    @Override
    public int getCount() {
        if (type == 1) {
            return id_list.size();
        } else if (id_list.size() == 0) {
            return 0;
        } else return count;
    }

    @Override
    public Object getItem(int position) {
        if (type == 1) {
            return id_list.get(position);
        } else return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();
            if (type == 1) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_car_find_1, parent, false);
                holder.id_tv = convertView.findViewById(R.id.id_tv);
                holder.count_tv = convertView.findViewById(R.id.count_tv);
                holder.money_tv = convertView.findViewById(R.id.money_tv);
                holder.minus = convertView.findViewById(R.id.minus);
            } else {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_car_find_2, parent, false);
                holder.time_tv = convertView.findViewById(R.id.time_tv);
                holder.road_tv = convertView.findViewById(R.id.road_tv);
                holder.info_tv = convertView.findViewById(R.id.info_tv);
                holder.money_tv = convertView.findViewById(R.id.money_tv);
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        PeccancyTypeBean.ROWSDETAILBean typeBean = type_list.get(0);
        for (int i = 0; i < type_list.size(); i++) {
            if (type_list.get(i).getPcode().equals(list.get(position).getPcode())) {
                typeBean = type_list.get(i);
                break;
            }
        }

        if (type == 1) {
            holder.id_tv.setText(id_list.get(position));
            holder.count_tv.setText("未处理违章 " + list.size() + " 次");
            holder.money_tv.setText("扣 " + typeBean.getPscore() + " 分      罚款 " + typeBean.getPmoney() + " 元");
            holder.minus.setOnClickListener(v -> {
                CarFindActivity.id_list.remove(position);
                calcCount();
                getCount();
                notifyDataSetChanged();
            });
        } else {
            convertView.setOnClickListener(v -> CarFindActivity.carFindActivity.showView(3));
            holder.time_tv.setText(list.get(position).getPdatetime());
            holder.road_tv.setText(list.get(position).getPaddr());
            holder.info_tv.setText(typeBean.getPremarks());
            holder.money_tv.setText("扣 " + typeBean.getPscore() + " 分      罚款 " + typeBean.getPmoney() + " 元");
        }

        return convertView;
    }

    static class ViewHolder {
        TextView id_tv, count_tv, money_tv;
        View minus;

        TextView time_tv, road_tv, info_tv;
    }
}

